<?php

namespace app\admin\controller;

use think\cache\driver\File;
use think\Controller;
use think\Db;
use think\Request;

class User extends AdminController
{
    public function index()
    {

        $user_list = Db::name('vip')
            ->paginate(3, false, ['query' => request()->param()]);
//        dump($user_list);die;
//        $status = Db::name('baoming')
//            ->where('')
        $page = $user_list->render();
        return view('user/index', ['data' => $user_list, 'page' => $page]);
    }

    public function edit(Request $request, $uid)
    {
        $user_info = Db::name('vip')
            ->where('uid', $uid)
            ->find();
        return view('user/edit', ['data' => $user_info]);
    }

    public function add(Request $request)
    {
        return view();
    }

    public function doAdd()
    {
        $data = [
            'username' => input('username'),
            'uname' => input('uname'),
            'uemail' => input('uemail'),
            'utel' => input('utel'),
            'qq' => input('qq'),
            'uage' => input('uage'),
            'courseid' => input('courseid'),
            'classid' => input('classid'),
            'referid' => input('referid'),
            'status' => input('status')
        ];
        Db::name('vip')
            ->insert($data);
        $this->success('添加成功');
    }

    public function doEdit()
    {
        $im = request()->file('uimg');
        if (!empty($im)) {
            $image = \think\Image::open($im);
            $info = $im->validate(['size' => 105678, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if (!$info) {
                $this->error('头像大小应在100K以内，仅支持jpg,png,gif格式');
            }
        }
        if (isset($image)) {
            $filename = rand(1000, 9999) . time();
            $font = ROOT_PATH . 'public' . DS . 'static/admin/main/assets/fonts/fontawesome-webfont.ttf';
            $img = ROOT_PATH . 'public' . DS . 'uploads' . DS . date('Ymd') . DS . $filename . '.jpg';
            $imgPath = date('Ymd') . DS . $filename . '.jpg';
            $image->thumb(350, 350, \think\Image::THUMB_CENTER)->text('兄弟连“狠”爱你', $font, 20, '#ff0000')->save($img);
        }
        if (!isset($imgPath)) {
            $data = [
                'uname' => input('uname'),
                'uemail' => input('uemail'),
                'qq' => input('qq'),
                'uage' => input('uage'),
                'courseid' => input('courseid'),
                'classid' => input('classid'),
                'status' => input('status')
            ];
        } else {
            $data = [
                'uname' => input('uname'),
                'uemail' => input('uemail'),
                'qq' => input('qq'),
                'uage' => input('uage'),
                'courseid' => input('courseid'),
                'classid' => input('classid'),
                'uimg' => $imgPath,
                'status' => input('status')
            ];
        }
        $updateRes = Db::name('vip')
            ->where('uid', input('uid'))
            ->update($data);
        if ($updateRes) {
            $this->success('编辑成功', url('admin/User/index'));
        } else {
            $this->success('您没有做任何编辑');
        }
    }

    public function joinBlack()
    {
        $res = Db::name('vip')
            ->where('uid', input('uid'))
            ->update(['black' => 1]);
        dump($res);
    }

    public function outBlack()
    {
        $res = Db::name('vip')
            ->where('uid', input('uid'))
            ->update(['black' => 0]);
        dump($res);
    }

// <<<<<<< HEAD
//     // public function cate()
//     // {
//     //     if(input('status') == -1){
//     //         $res = Db::name('vip')
//     //             ->select();
//     //     }else{
//     //         $res = Db::name('vip')
//     //             ->where('status',input('status'))
//     //             ->select();
//     //     }
//     //     return view('user/cate',['data'=>$res]);
//     // }
// =======
    public function cate()
    {
        if (input('status') == -1) {
            $res = Db::name('vip')
                ->select();
        } else {
            $res = Db::name('vip')
                ->where('status', input('status'))
                ->select();
        }
        return view('user/cate', ['data' => $res]);
    }

    public function course()
    {
        $uid = input('uid');
        $sql = 'select a.coursename, b.classname, c.name, d.fee, d.refertel from oa_baoming as d left join oa_course as a on d.courseid = a.id left join oa_classdate as b on b.id = d.classid left join oa_activity as c on c.id = d.actid where uid = ' . $uid;
        $info = Db::query($sql);
        return view('',['info'=>$info]);
    }

}
